package com.jk.vehicle.mini.controller.base;

import com.jk.vehicle.mini.entity.User;
import com.jk.vehicle.mini.entity.UserBicycleLink;
import com.jk.vehicle.mini.service.CorpService;
import com.jk.vehicle.mini.service.UserBicycleLinkService;
import com.jk.vehicle.core.controller.BaseController;
import com.jk.vehicle.core.entity.ResponseEntity;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jk.vehicle.mini.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Date;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * This is Description for UserBicycleLinkController
 *
 * @author Administrator
 * @date 2021/02/08
 */
@Api(value = "人员车辆关联信息相关接口", tags = "人员车辆关联信息相关接口")
@RestController
@RequestMapping("/user/bicycle/link")
public class UserBicycleLinkController extends BaseController {

	/**
	 * UserBicycleLinkService服务
	 */
    @Resource
    private UserBicycleLinkService userBicycleLinkService;

    @Resource
    private CorpService corpService;

    @Resource
    private UserService userService;

	/**
	 * 删除userBicycleLink√
	 * @param id
	 * @return
	 */
	@ApiOperation("根据主键删除关联数据")
    @DeleteMapping("/{id}/{corpId}")
    public ResponseEntity delete(@PathVariable Long id, @PathVariable Long corpId) {
        if (corpId == null || corpId.longValue() == 0) {
            return failResult("厂家id不能为空！");
        }
        if (corpId == -1) {
            return failResult("厂家id不能等于-1！");
        }
        List<String> dsNameList = new ArrayList<>();
        String dsName = corpService.selectDsNameById(corpId);
        dsNameList.add(null);//null：代表通用数据源，非空代表独立数据源
        dsNameList.add(dsName);
        for (String dbName: dsNameList) {
            userBicycleLinkService.deleteByIdPermanent(id, dbName);
        }
        return successResult();
    }

	/**
	 * 更新userBicycleLink√
	 * @param userBicycleLink
	 * @return
	 */
    @ApiOperation(value = "更新用户车辆关联")
    @PutMapping
    public ResponseEntity update(@RequestBody UserBicycleLink userBicycleLink) {
        if (userBicycleLink.getCorpId().longValue() == 0 || userBicycleLink.getCorpId() == null) {
            return failResult("厂家id不能为空！");
        }
        if (userBicycleLink.getCorpId() == -1) {
            return failResult("厂家id不能等于-1！");
        }
        User user = userService.findById(userBicycleLink.getUserId(), null);
        if (user == null) {
            return failResult("该用户不存在！");
        }
        List<String> dsNameList = new ArrayList<>();
        String dsName = corpService.selectDsNameById(userBicycleLink.getCorpId());
        dsNameList.add(null);//null：代表通用数据源，非空代表独立数据源
        dsNameList.add(dsName);
        userBicycleLink.setUpdateDate(new Date());
        for (String dbName: dsNameList) {
            userBicycleLinkService.update(userBicycleLink, dbName);
        }
        return successResult(userBicycleLink);
    }
}
